package cc.linker.steplink.crm.task;


import cc.linker.steplink.crm.commons.CrmRedisKeys;
import cc.linker.steplink.crm.commons.lock.RedisLock;
import cc.linker.steplink.crm.merchanth5.dao.MallDao;
import cc.linker.steplink.crm.merchanth5.service.MallService;
import cc.linker.steplink.crm.merchanth5.service.redis.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Set;

@Component
public class GoodsSkuLockTask {


    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private MallDao mallDao;
    @Autowired
    private MallService mallService;
    @Autowired
    private GoodsService goodsService;

    @Async
    @RedisLock(value = "goodsSkuLockTask")
    @Scheduled(cron = "0 0 0/2 * * ?")
    public void goodsSkuLockTask() {
        List<Map<String, Object>> allGoodsList = mallDao.getAllGoodsList();
        for(Map<String, Object> da:allGoodsList){
            String goodsCountKey = CrmRedisKeys.goodsCountKey + da.get("goodsId").toString();
            Boolean aBoolean = stringRedisTemplate.hasKey(goodsCountKey);
            if(null == aBoolean || !aBoolean){
                mallService.flushGoodsCache(false,Long.valueOf(da.get("goodsId").toString()),null,Long.valueOf(da.get("goodsCount").toString()));
            }
        }

        List<Map<String, Object>> allSkuList = mallDao.getAllSkuList();
        for(Map<String, Object> ad:allSkuList){
            String skuCountKey = CrmRedisKeys.skuCountKey + ad.get("goodsId").toString() + "_" + ad.get("skuId").toString();
            Boolean aBoolean = stringRedisTemplate.hasKey(skuCountKey);
            if(null == aBoolean || !aBoolean){
                mallService.flushGoodsCache(false,Long.valueOf(ad.get("goodsId").toString()),ad.get("skuId").toString(),Long.valueOf(ad.get("goodsCount").toString()));
            }
        }

    }

    @Async
    @RedisLock(value = "goodsSkuLockTaskFlush")
    @Scheduled(cron = "55 59 3 * * ?")
    public void goodsSkuLockTaskFlush() {
        String goodsCountKey = CrmRedisKeys.goodsCountKey + "*";
        Set<String> gKeys = stringRedisTemplate.keys(goodsCountKey);
        stringRedisTemplate.delete(gKeys);
        String skuCountKey = CrmRedisKeys.skuCountKey + "*";
        Set<String> sKeys = stringRedisTemplate.keys(skuCountKey);
        stringRedisTemplate.delete(sKeys);
    }


    public void flushAllGoodsCache(){
        String goodsInfoKey = CrmRedisKeys.GOODS_INFO_KEY + "*";
        Set<String> keys1 = stringRedisTemplate.keys(goodsInfoKey);
        String skuInfoKey = CrmRedisKeys.GOODS_SKU_INFO_KEY + "*";
        Set<String> keys2 = stringRedisTemplate.keys(skuInfoKey);
        String specInfoKey = CrmRedisKeys.GOODS_SPEC_INFO_KEY + "*";
        Set<String> keys3 = stringRedisTemplate.keys(specInfoKey);
        String goodsSkuOldKey = CrmRedisKeys.cartGoodsSkuInfoKey + "*";
        Set<String> keys4 = stringRedisTemplate.keys(goodsSkuOldKey);
        String goodsCountKey = CrmRedisKeys.goodsCountKey + "*";
        Set<String> keys5 = stringRedisTemplate.keys(goodsCountKey);
        String skuCountOldKey = CrmRedisKeys.skuCountKey + "*";;
        Set<String> keys6 = stringRedisTemplate.keys(skuCountOldKey);
        String columnContentsKey = CrmRedisKeys.COLUMN_CONTENTS_KEY + "*";;
        Set<String> keys7 = stringRedisTemplate.keys(columnContentsKey);
        stringRedisTemplate.delete(keys1);
        stringRedisTemplate.delete(keys2);
        stringRedisTemplate.delete(keys3);
        stringRedisTemplate.delete(keys4);
        stringRedisTemplate.delete(keys5);
        stringRedisTemplate.delete(keys6);
        stringRedisTemplate.delete(keys7);
    }
}
